package com.shanghaionstar.mro.view;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.os.Handler;
import android.util.AttributeSet;
import android.view.View;

import com.shanghaionstar.R;
import com.shanghaionstar.mro.OnClickListene;
import com.shanghaionstar.mro.util.BitmapZoomUtils;

public class Round extends View {
	private Paint paint;
	private int centerX;
	private int centerY;
	private int angleOne, angleTwo, angleThree, angleFour;
	private int radiusOne, radiusTwo, radiusThree, radiusFour;
	private Boolean isBegin = false;
	private Boolean isReduce = false;
	private Boolean isNext = false;
	public static OnClickListene listene;
	private int reduceOne, reduceTwo, reduceThree, reduceFour;

	public Boolean getIsBegin() {
		return isBegin;
	}

	public void setIsBegin(Boolean isBegin) {
		this.isBegin = isBegin;
	}

	public Boolean getIsReduce() {
		return isReduce;
	}

	public void setIsReduce(Boolean isReduce) {
		this.isReduce = isReduce;
	}

	// 4个光点x,y坐标
	private float xOne, xTwo, xThree, xFour, yOne, yTwo, yThree, yFour;
	private Bitmap bitmapOne, bitmapTwo, bitmapThree, bitmapFour;
	Handler handler = new Handler();
	BitmapZoomUtils utils = new BitmapZoomUtils();
	Runnable runnable = new Runnable() {

		@Override
		public void run() {
			if (isNext) {
				isBegin = false;
				handler.post(null);
				handler.removeCallbacks(runnable);
				releaseBitmap(bitmapOne);
				releaseBitmap(bitmapTwo);
				releaseBitmap(bitmapThree);
				releaseBitmap(bitmapFour);
			} else {
				AddAngle();
				getR();
				invalidate();
				handler.postDelayed(runnable, 40);
			}
		}
	};

	protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
		// radiusOne = getWidth() / 2 / 10 * 9;
		// radiusTwo = getWidth() / 2 / 10 * 8;
		// radiusThree = getWidth() / 2 / 10 * 7;
		// radiusFour = getWidth() / 2 / 10 * 6;
		setMeasuredDimension(getDefaultSize(getSuggestedMinimumWidth(), widthMeasureSpec),
				getDefaultSize(getSuggestedMinimumHeight(), heightMeasureSpec));
		
	}

	/**
	 * 初始化
	 */
	public void initView() {
		paint = new Paint();
		BitmapZoomUtils utils = new BitmapZoomUtils();
		bitmapOne = utils.getZoom(BitmapFactory.decodeResource(getResources(), R.drawable.mro_luminous_point1), 0.8f,
				0.8f);
		bitmapTwo = utils.getZoom(BitmapFactory.decodeResource(getResources(), R.drawable.mro_luminous_point2), 0.8f,
				0.8f);
		bitmapThree = utils.getZoom(BitmapFactory.decodeResource(getResources(), R.drawable.mro_luminous_point3), 0.8f,
				0.8f);
		bitmapFour = utils.getZoom(BitmapFactory.decodeResource(getResources(), R.drawable.mro_luminous_point4), 0.8f,
				0.8f);
		angleOne = 0;
		angleTwo = 45;
		angleThree = 90;
		angleFour = 135;
		isBegin = false;
		isReduce = false;
		xOne = 0;
		xTwo = 0;
		xThree = 0;
		xFour = 0;
		yOne = 0;
		yTwo = 0;
		yThree = 0;
		yFour = 0;
		centerX = 0;
		centerY = 0;
		isBegin = false;
		isReduce = false;
		isNext = false;
		
//		radiusOne = 500;
//		radiusTwo = 460;
//		radiusThree = 420;
//		radiusFour = 380;
//		reduceOne = radiusOne / 15;
//		reduceTwo = radiusTwo / 15;
//		reduceThree = radiusThree / 15;
//		reduceFour = radiusFour / 15;
		
		
		radiusOne = 500;
		radiusTwo = 440;
		radiusThree = 380;
		radiusFour = 320;
//		reduceOne = radiusOne / 15;
//		reduceTwo = radiusTwo / 15;
//		reduceThree = radiusThree / 15;
//		reduceFour = radiusFour / 15;
		reduceOne = 20;
		reduceTwo = 18;
		reduceThree = 16;
		reduceFour = 14;

		handler.removeCallbacks(runnable);
		handler.post(runnable);
		// if (isNext) {
		// handler.post(null);
		// handler.removeCallbacks(runnable);
		// }
	}

	public Round(Context context) {
		super(context);

	}

	public Round(Context context, AttributeSet attrs) {
		super(context, attrs);
	}

	public Round(Context context, AttributeSet attrs, int defStyleAttr) {
		super(context, attrs, defStyleAttr);
	}

	public void stop() {
		handler.removeCallbacks(runnable);
	}

	// 从你回掉的类里面注入一个，实现了该接口的实例过来，否则NullPoint
	public void setOnClickListene(OnClickListene onClickListene) {
		listene = onClickListene;
	}

	/**
	 * 画图
	 */
	@Override
	protected void onDraw(Canvas canvas) {
		super.onDraw(canvas);
		int height = getHeight();
		int width = getWidth();
		centerX = width / 2;
		centerY = height / 2;
		xOne = centerX + getRoundX(angleOne, radiusOne);
		yOne = centerY + getRoundY(angleOne, radiusOne);
		xTwo = centerX + getRoundX(angleTwo, radiusTwo);
		yTwo = centerY + getRoundY(angleTwo, radiusTwo);
		xThree = centerX + getRoundX(angleThree, radiusThree);
		yThree = centerY + getRoundY(angleThree, radiusThree);
		xFour = centerX + getRoundX(angleFour, radiusFour);
		yFour = centerY + getRoundY(angleFour, radiusFour);
		if (isBegin) {
			drawBitmap(canvas, bitmapOne, xOne, yOne);
			drawBitmap(canvas, bitmapTwo, xTwo, yTwo);
			drawBitmap(canvas, bitmapThree, xThree, yThree);
			drawBitmap(canvas, bitmapFour, xFour, yFour);
		}
	}

	private void AddAngle() {
		angleOne = (angleOne + 1) % 360;
		angleTwo = (angleTwo + 1) % 360;
		angleThree = (angleThree + 1) % 360;
		angleFour = (angleFour + 1) % 360;
	}

	/**
	 * 4个光点绕圈的半径
	 */
	private void getR() {
		if (isReduce) {
			if (radiusOne > 0) {
				radiusOne = radiusOne - reduceOne;
				radiusTwo = radiusTwo - reduceTwo;
				radiusThree = radiusThree - reduceThree;
				radiusFour = radiusFour - reduceFour;
			} else if (radiusOne <= 0) {
				try {
					isNext = true;
					listene.onCallBackIsShow(isNext);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}

		}
	}

	/**
	 * bitmap释放
	 */
	private void releaseBitmap(Bitmap bitmap) {
		if (bitmap != null && !bitmap.isRecycled()) {
			bitmap.recycle();
			bitmap = null;
		}
	}

	/**
	 * 得到绕圈Y坐标
	 * 
	 * @param angle
	 * @param r
	 * @return
	 */
	private float getRoundY(int angle, int radius) {
		return (float) (Math.cos((2 * Math.PI / 360) * 6 * angle) * radius);
	}

	/**
	 * 得到绕圈X坐标
	 * 
	 * @param angle
	 * @param r
	 * @return
	 */
	private float getRoundX(int angle, int radius) {
		return (float) (Math.sin((2 * Math.PI / 360) * 6 * angle) * radius);
	}

	/**
	 * 画点
	 * 
	 * @param canvas
	 * @param bitmap
	 * @param x
	 * @param y
	 */
	private void drawBitmap(Canvas canvas, Bitmap bitmap, float x, float y) {
		canvas.drawBitmap(bitmap, x - (bitmap.getWidth() / 2), y - (bitmap.getHeight() / 2), paint);
	}
}
